﻿using System;
using System.Configuration;
using System.Data.Linq;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class EvectionEdit : System.Web.UI.Page
    {
        protected ReqEvectionMain reqEvectionMain;
        protected string empName;
        private ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                string formNo = Request.QueryString["formNo"];
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<ReqEvectionMain>(r => r.ReqEvectionSchedules);
                dlo.LoadWith<ReqEvectionMain>(r => r.ReqEvectionVoyages);
                dlo.LoadWith<ReqEvectionMain>(r => r.ReqEvectionFlighters);
                ctx.LoadOptions = dlo;
                reqEvectionMain = ctx.ReqEvectionMains.Single(r => r.FormNo == formNo);

                FormNo.Value = reqEvectionMain.FormNo;
                PreCost.Value = reqEvectionMain.PreCost.ToString();
                TriperIds.Value = reqEvectionMain.TriperIds;
                Cause.Value = reqEvectionMain.Cause;

                rptList.DataSource = reqEvectionMain.ReqEvectionSchedules;
                rptList.DataBind();
                rptVoyage.DataSource = reqEvectionMain.ReqEvectionVoyages;
                rptVoyage.DataBind();
                rptFlighter.DataSource = reqEvectionMain.ReqEvectionFlighters;
                rptFlighter.DataBind();
                Employee employee = ctx.Employees.Single(r => r.Id == reqEvectionMain.EmpId);
                empName = employee.Name;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            _formSave(0);
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            _formSave(1);
        }
        private void _formSave(int isSubmit)
        {
            DataLoadOptions dlo = new DataLoadOptions();
            dlo.LoadWith<ReqEvectionMain>(r => r.ReqEvectionSchedules);
            dlo.LoadWith<ReqEvectionMain>(r => r.ReqEvectionVoyages);
            dlo.LoadWith<ReqEvectionMain>(r => r.ReqEvectionFlighters);
            ctx.LoadOptions = dlo;
            reqEvectionMain = ctx.ReqEvectionMains.Single(r => r.FormNo == FormNo.Value);
            reqEvectionMain.PreCost = decimal.Parse(PreCost.Value);
            reqEvectionMain.TriperIds = TriperIds.Value;
            reqEvectionMain.Cause = Cause.Value;
            reqEvectionMain.StatusId = isSubmit;
            if(reqEvectionMain.ReqEvectionSchedules.Count==0)
            {
                reqEvectionMain.StatusId = 0;
            }

            //更新时删除之前的签核数据
            var list = from r in ctx.SignFlows where r.FormNo == FormNo.Value select r;
            ctx.SignFlows.DeleteAllOnSubmit(list);

            ctx.SubmitChanges();
            if (isSubmit == 1)//写签核流程
            {
                FlowSet fs = new FlowSet();
                EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                fs.SetSignFlow(FormNo.Value, ebi);
            }
            Response.Write("<script>alert('修改成功');window.close();</script>");
        }
    }
}